package com.frank.common.utils;

import java.util.Random;

/**
 * @author Frank Zou
 * @version v0.0.1
 * @date 2024/2/21
 * @description
 */
public class StringUtil {

    /**
     * 获取一个验证码
     *
     * @param length 验证码长度
     * @return captcha code
     */
    public static String generateCaptchaCode(int length) {
        // 可选的字符集合，包含数字和字母
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        // 从字符集合中随机选择字符，组成验证码
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }
        return sb.toString();
    }
}
